//
//  BDShopReceiveHongbaoView.m
//  iOS_beboy
//
//  Created by wen on 2017/9/15.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopReceiveHongbaoView.h"
#import "BDShopReceiveHongbaoCell.h"

@interface BDShopReceiveHongbaoView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIImageView *contentImageView;
@property (nonatomic, strong) UIImageView *tableViewBKImage;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *sureButton;
@property (nonatomic, strong) UILabel *tishiLabel;

@property (nonatomic, strong) NSArray *datasource;

@end

@implementation BDShopReceiveHongbaoView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _bkView = [[UIView alloc] initWithFrame:self.bounds];
        _bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        [self addSubview:_bkView];
        
        _contentImageView = [[UIImageView alloc] init];
        _contentImageView.image = [UIImage imageNamed:@"hongbao_bg1"];
        _contentImageView.width = 0;
        _contentImageView.height = 0;
        _contentImageView.center = _bkView.center;
        [self addSubview:_contentImageView];
        
        _tableViewBKImage = [[UIImageView alloc] init];
        _tableViewBKImage.size = CGSizeZero;
        _tableViewBKImage.center = _contentImageView.center;
        _tableViewBKImage.image = [UIImage imageNamed:@"hongbao_bg2"];
        [self addSubview:_tableViewBKImage];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.sectionFooterHeight = 0;
        _tableView.sectionHeaderHeight = 0;
        _tableView.size = CGSizeZero;
        _tableView.center = _contentImageView.center;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.bounces = NO;
        [_tableView registerClass:[BDShopReceiveHongbaoCell class] forCellReuseIdentifier:@"redEnvelopeCell"];
        [self addSubview:_tableView];
        
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureButton.size = CGSizeZero;
        _sureButton.center = _contentImageView.center;
        _sureButton.backgroundColor = [UIColor yellowColor];
        [_sureButton setTitle:ASLocalizedString(@"点击确定")forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor colorWithHexString:@"f03d31"] forState:UIControlStateNormal];
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _sureButton.layer.masksToBounds = YES;
        _sureButton.layer.cornerRadius = 4;
        [self addSubview:_sureButton];
        
        [[_sureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self hidden];
        }];
        
        _tishiLabel = [[UILabel alloc] init];
        _tishiLabel.text = ASLocalizedString(@"红包可抵扣扫码支付时的订单金额");
        _tishiLabel.font = [UIFont systemFontOfSize:13];
        _tishiLabel.textColor = [UIColor whiteColor];
        _tishiLabel.size = CGSizeZero;
        _tishiLabel.textAlignment = NSTextAlignmentCenter;
        _tishiLabel.center = _contentImageView.center;
        [self addSubview:_tishiLabel];
    }
    return self;
}

- (void)hidden{
    [UIView animateWithDuration:0.25 animations:^{
        _bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        
        _contentImageView.width = 0;
        _contentImageView.height = 0;
        _contentImageView.center = _bkView.center;
        
        _tableViewBKImage.size = CGSizeZero;
        _tableViewBKImage.center = _contentImageView.center;
        
        _tableView.size = CGSizeZero;
        _tableView.center = _contentImageView.center;
        
        _sureButton.size = CGSizeZero;
        _sureButton.center = _contentImageView.center;
        
        _tishiLabel.textColor = [UIColor whiteColor];
        _tishiLabel.size = CGSizeZero;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

+ (void)showWithDatasource:(NSArray *)datasource{
    BDShopReceiveHongbaoView *showView = [[BDShopReceiveHongbaoView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.datasource = datasource;
    [showView.tableView reloadData];
    
    if (datasource.count) {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 0.1)];
        headView.backgroundColor= [UIColor clearColor];
        showView.tableView.tableHeaderView = headView;
        
    }else{
        UIView *headView = [[UIView alloc] initWithFrame:showView.tableView.bounds];
        headView.backgroundColor = [UIColor clearColor];
        UILabel *label = [[UILabel alloc] init];
        label.text = ASLocalizedString(@"不好意思哦，红包已经被领完啦");
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor colorWithHexString:@"#808080"];
        [label sizeToFit];
        label.center = CGPointMake(headView.width / 2.0, headView.height / 2.0);
        [headView addSubview:label];
        
        showView.tableView.tableHeaderView = headView;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:showView];
    [UIView animateWithDuration:0.25 animations:^{
        showView.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        
        showView.contentImageView.width = 332 / 1.0 / kBDScreenWidth * 375;
        showView.contentImageView.height = 440 / 1.0 / kBDScreenWidth * 375;
        showView.contentImageView.center = showView.bkView.center;
        
        showView.tableViewBKImage.size = CGSizeMake(312 / 1.0 / kBDScreenWidth * 375, 312 / 1.0 / kBDScreenWidth * 375);
        showView.tableViewBKImage.centerX = showView.contentImageView.centerX;
        showView.tableViewBKImage.top = showView.contentImageView.top + 30;
        
        showView.tableView.size = CGSizeMake(showView.tableViewBKImage.width, showView.tableViewBKImage.height - 15);
        showView.tableView.centerX = showView.contentImageView.centerX;
        showView.tableView.top = showView.tableViewBKImage.top + 5;
        
        if (showView.tableView.tableHeaderView) {
            showView.tableView.tableHeaderView.frame = showView.tableView.bounds;
            for (UIView *subView in showView.tableView.tableHeaderView.subviews) {
                subView.center = CGPointMake(showView.tableView.tableHeaderView.width / 2.0, showView.tableView.tableHeaderView.height / 2.0 - 30);
            }
        }
        
        showView.sureButton.size = CGSizeMake(showView.tableView.width - 10, 40);
        showView.sureButton.top = showView.tableViewBKImage.bottom + 12.5;
        showView.sureButton.centerX = showView.tableView.centerX;
        
        [showView.tishiLabel sizeToFit];
        showView.tishiLabel.top = showView.sureButton.bottom + 10;
        showView.tishiLabel.centerX = showView.sureButton.centerX;
    } completion:^(BOOL finished) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datasource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 68;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor clearColor];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BDShopReceiveHongbaoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"redEnvelopeCell" forIndexPath:indexPath];
    [cell configeWithModel:self.datasource[indexPath.section]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
